
var TaskDefinition = new Object();

TaskDefinition.createModel = function(task) {
	var model = new Object();
	model.getTask = function() {return task;};
	return model;
}

TaskDefinition.createView = function(model) {
	var taskView = new WindView();
	taskView.setCssClass("TaskDefinition");

	var taskActionsView = TaskDefinition.createActionsView(model);
	taskView.addView(taskActionsView);
	
	var bodyView = new WindView();
	bodyView.hide();
	
	var headerView = TaskDefinition.createHeaderView(model, bodyView);
	taskView.addView(headerView);
	
	taskView.addView(bodyView);
	
	var descriptionView = TaskDefinition.createDescriptionView(model);
	bodyView.addView(descriptionView);
	
	var childrenView = TaskDefinition.createChildrenView(model);
	bodyView.addView(childrenView);
	
	return taskView;
}

TaskDefinition.createHeaderView = function(model, bodyView) {
	var view = new WindView();
	view.setCssClass("TaskDefinition_Header");
	
	var layoutView = WindLayout.createView();
	view.addView(layoutView);
	
	var rowView = layoutView.createRowView();
	
	var collapseView = new WindView();
	var expandView = new WindView();
	
	{
		var cellView = rowView.createCellView();
		
		collapseView.setCssClass("TaskDefinition_Header_Collapse");
		collapseView.setImage("wind/img/WindCollapse.gif");
		collapseView.hide();
		
		collapseView.onClick = function(e) {
			collapseView.hide();
			expandView.show();
			bodyView.hide();
		}
		
		cellView.addView(collapseView);

		expandView.setCssClass("TaskDefinition_Header_Expand");
		expandView.setImage("wind/img/WindExpand.gif");

		expandView.onClick = function(e) {
			collapseView.show();
			expandView.hide();
			bodyView.show();
		}
		
		cellView.addView(expandView);
	}

	{
		var cellView = rowView.createCellView();
		
		var readView = new WindView();
		readView.setCssClass("TaskDefinition_TitleRead");
		readView.setText(model.getTask().getTitle());
		cellView.addView(readView);
		
		var editView = WindText.createView();
		editView.setCssClass("TaskDefinition_TitleEdit");
		editView.setLines(1);
		editView.hide();
		cellView.addView(editView);
		
		readView.onClick = function(e) {
			var title = model.getTask().getTitle();
			editView.getHtmlElement().value = title;
			readView.hide();
			editView.show();
			editView.getHtmlElement().focus();
		}
		
		editView.commitChanges = function() {
			var title = editView.getHtmlElement().value;
			model.getTask().setTitle(title);	
			readView.setText(title);
			editView.hide();
			readView.show();
		}
		
		editView.onBlur = function(e) {
			editView.commitChanges();
		}
		
		editView.onKeyPress = function(e) {
			if(e.which==13) {
				editView.commitChanges();
			} 
		}
	}	
	
	return view;
}

TaskDefinition.createDescriptionView = function(model) {
	var view = new WindView();
	view.setCssClass("TaskDefinition_Description");
	
	var readView = new WindView();
	readView.setCssClass("TaskDefinition_DescriptionRead");
	readView.setText(model.getTask().getDescription());
	view.addView(readView);
	
	var editView = WindText.createView();
	editView.setCssClass("TaskDefinition_DescriptionEdit");
	editView.setLines(10);
	editView.hide();
	view.addView(editView);
	
	readView.onClick = function(e) {
		var descWithoutBR = model.getTask().getDescription();
		editView.getHtmlElement().value = descWithoutBR;
		readView.hide();
		editView.show();
		editView.getHtmlElement().focus();
	}
	
	editView.commitChanges = function() {
		var descWithoutBR = editView.getHtmlElement().value;
		model.getTask().setDescription(descWithoutBR);	
		var descWithBR = descWithoutBR.replace(/\n/g, "<br/>");
		readView.setText(descWithBR);
		editView.hide();
		readView.show();
	}
	
	editView.onBlur = function(e) {
		editView.commitChanges();
	}
	
	return view;
}

TaskDefinition.createChildrenView = function(model) {
	var view = new WindView();
	view.setCssClass("TaskDefinition_Children");
	
	var task = model.getTask();
	
	var empty = task.getTasks().length == 0;
	
	if(empty) {
		var addChildButtonView = TaskDefinition.createAddChildButtonView();
		view.addView(addChildButtonView);
	} else {
		for(var i=0; i<task.getTasks().length; i++) {
			var childTask = task.getTasks()[i];
			var childView = TaskDefinition.createChildView(childTask);
			view.addView(childView);
		}
	}

	
	return view;
}

TaskDefinition.createAddChildButtonView = function(task) {
	var view = new WindView();
	view.setCssClass("TaskDefinition_AddChildButton");
	
	view.setText("Click here to add a new child");
	
	return view;
}

TaskDefinition.createChildView = function(task) {
	var view = new WindView();
	view.setCssClass("TaskDefinition_Child");
	
	view.setText(task.getTitle());
	
	return view;
}
	
TaskDefinition.createActionsView = function(model) {
	var actionsModel = new WindPanelModel();
	actionsModel.addTextEntry("Save")
	actionsModel.addTextEntry("Reload")
	actionsModel.addTextEntry("Delete")
	actionsModel.addTextEntry("Print")
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Planning");
	actionsModel.addTextEntry("Tracking");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Follow");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Versions");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Roles");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Links");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Attachments");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Comments");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("Tags");
	actionsModel.addSeparator();
	actionsModel.addTextEntry("History");

	var actionsView = WindPanel.createView(actionsModel);
	actionsView.setId("TaskDefinition_Actions");
	return actionsView;
}
